package csc.daonjpa.java.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import csc.daonjpa.java.dao.AccountDAO;
import csc.daonjpa.java.dao.CustomerDAO;
import csc.daonjpa.java.dao.TargetAccountDAO;
import csc.daonjpa.java.dao.TargetAccountDAO;
import csc.daonjpa.java.dao.TransactionDAO;

import csc.daonjpa.java.domain.Account;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.TargetAccount;
import csc.daonjpa.java.domain.TargetAccount;
import csc.daonjpa.java.domain.Transaction;

@Controller
public class TransactionController {

	@RequestMapping(value = "/addNewTransaction")
	public ModelAndView addNewTransaction(HttpServletRequest request,
			Model model) {

		String message = "";
		TargetAccountDAO targetDAO = new TargetAccountDAO();
		AccountDAO accountDao = new AccountDAO();
		TransactionDAO transDAO = new TransactionDAO();
		ModelAndView modelview = new ModelAndView("Managetransaction");

		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		try {
			List<TargetAccount> listTarget = targetDAO
					.getTargetAccounts(loginId);
			model.addAttribute("listTargetAccount", listTarget);
		} catch (Exception e) {
			message = "List Target Account get Error";
			model.addAttribute("message", message);
			return modelview;
		}

		try {
			List<Account> accountList = accountDao.getAccounts(loginId);
			model.addAttribute("listAccount", accountList);
		} catch (Exception e) {
			message = "List Account get Error";
			model.addAttribute("message", message);
			return modelview;
		}
		try {
			List<Transaction> listTransaction = transDAO
					.getAllTransaction(loginId);
			model.addAttribute("listTransaction", listTransaction);
		} catch (Exception e) {
			message = "List Transaction get Error";
			model.addAttribute("message", message);
			return modelview;
		}
		return modelview;
	}

	@RequestMapping(value = "/TargetAccountTransaction")
	public String targetAccountTransaction(HttpServletRequest request,
			Model model) {

		TargetAccountDAO targetDAO = new TargetAccountDAO();
		TransactionDAO transDAO = new TransactionDAO();
		AccountDAO accDAO = new AccountDAO();
		// Read account info from request and save into Account object
		int avai = 0;
		String loginId = request.getSession().getAttribute("loginSession")
				.toString();
		String accNo = request.getParameter("accountNumber");

		String message = "";
		String targetAccNo = request.getParameter("accountTargetNumber");
		String targetAccName = "";
		String targetAccBank = "";
		String targetAccBranch = "";

		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		String dateTrans = dateFormat.format(date);

		int amount = Integer.parseInt(request.getParameter("amount"));

		List<TargetAccount> listtargetAcc = targetDAO
				.getTargetAccountInfo(targetAccNo);
		for (int i = 0; i < listtargetAcc.size(); i++) {
			targetAccName = listtargetAcc.get(i).getAccountOwner();
			targetAccBank = listtargetAcc.get(i).getBank();
			targetAccBranch = listtargetAcc.get(i).getBranch();
		}

		Account myAccount = accDAO.getAccountByAccno(accNo);
		if (amount > myAccount.getAvailableNumber()) {
			message = "Your available Amount is not enough!!!";
			model.addAttribute("message", message);
			return ("forward:/addNewTransaction.html");
		} else {
			avai = myAccount.getAvailableNumber() - amount;
			accDAO.checkUpdateAccount(avai, myAccount.getAccountNumber());

		}

		Transaction trans = new Transaction(0, targetAccBank, targetAccBranch,
				amount, dateTrans, accNo, targetAccNo);

		if (transDAO.addTransaction(trans)) {
			message = "Transaction to *_" + targetAccNo + "_* Successfully!!!";
		} else {
			message = "Transaction to *_" + targetAccNo + "_* Failed!!!";
		}
		model.addAttribute("message", message);
		return ("forward:/addNewTransaction.html");

	}

	@RequestMapping(value = "/normalAccountTransaction")
	public String normalAccountTransaction(HttpServletRequest request,
			Model model) {
		// Read account info from request and save into Account object
		String accNo = request.getParameter("accountNumber");
		String message = "";
		int avai = 0;
		String targetAccNo = request.getParameter("accountTargetNumber");
		String targetAccName = request.getParameter("accountTargetName");
		String targetAccBank = request.getParameter("accountTargetBank");
		String targetAccBranch = request.getParameter("accountTargetBranch");

		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		Date date = new Date();
		String dateTrans = dateFormat.format(date);

		int amount = Integer.parseInt(request.getParameter("amount"));

		TransactionDAO transDAO = new TransactionDAO();
		AccountDAO accDAO = new AccountDAO();
		TargetAccountDAO targetDAO = new TargetAccountDAO();

		String loginId = request.getSession().getAttribute("loginSession")
				.toString();

		Account myAccount = accDAO.getAccountByAccno(accNo);
		Account mytargetAccount = null;
		try {
			mytargetAccount = accDAO.getAccountByAccno(targetAccNo);
		} catch (Exception e) {
			message = "Target Account did not Found!!! ";
			model.addAttribute("message", message);
			return ("forward:/addNewTransaction.html");

		}
		if (targetAccName.equals(mytargetAccount.getAccountName())) {
			if (amount > myAccount.getAvailableNumber()) {
				message = "Your available Amount is not enough!!!";
				model.addAttribute("message", message);
				return ("forward:/addNewTransaction.html");
			} else {
				avai = myAccount.getAvailableNumber() - amount;
				accDAO.checkUpdateAccount(avai, myAccount.getAccountNumber());
				accDAO.checkUpdateAccount(mytargetAccount.getAvailableNumber()
						+ amount, targetAccNo);
			}
		}

		Transaction transaction = new Transaction(0, targetAccBank,
				targetAccBranch, amount, dateTrans, accNo, targetAccNo);
		System.out.println(transaction.toString());
		if (transDAO.addTransaction(transaction)) {
			message = "Transaction to *_" + targetAccNo + "_* Successfully!!!";
		} else {
			message = "Transaction to *_" + targetAccNo + "_* Failed!!!";
		}
		model.addAttribute("message", message);
		return ("forward:/addNewTransaction.html");

	}

}
